#!/bin/bash

current_dir=`cd $(dirname $0);pwd`

soft_dir=/opt/softs
[ -d ${soft_dir} ] || mkdir -p ${soft_dir}

jenkins_port=8888
install_dir=/opt/jenkins/

# http://mirrors.jenkins-ci.org/war/
# 先安装jdk(最新版本已经建议jdk11及以上,不建议最新)
yum install java-1.8.0-openjdk wget -y
[ -d ${install_dir} ] || mkdir -p ${install_dir}
[ -d ${install_dir}/data ] || mkdir -p ${install_dir}/data
[ -d ${install_dir}/logs ] || mkdir -p ${install_dir}/logs

cd  ${install_dir}
wget http://mirrors.jenkins-ci.org/war/latest/jenkins.war || exit 1
java -jar jenkins.war --httpPort=${jenkins_port} &
sleep 60
pid=`ps -ef | grep jenkins.war | grep -v grep | awk '{print $2}'`
[ -z $pid ] || kill -9 $pid

mv ~/.jenkins/* ${install_dir}/data
mv ${install_dir}/data/war ${install_dir}
echo -e "\033[31m next line is init admin password\033[0m"
cat ${install_dir}/data/secrets/initialAdminPassword

cp startup.sh ${soft_dir}
sed -i "s/8888/${jenkins_port}/g" ${soft_dir}/startup.sh
chmod +x ${soft_dir}/startup.sh